﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Newtonsoft.Json.Linq;
using System.Data.Common;
using System.IO;
using System.Reflection;
using System.Reflection.Metadata;
using Wpf.Ui.Appearance;
using Wpf.Ui.Controls;
using WpfMvvmTemplate.WPFUI.Common;

namespace WpfMvvmTemplate.WPFUI.ViewModels.Pages
{
    public partial class SettingsViewModel : ObservableObject, INavigationAware
    {
        private bool isInitialized = false;

        [ObservableProperty]
        private string appVersion = string.Empty;

        [ObservableProperty]
        private ApplicationTheme currentTheme = ApplicationTheme.Light;

        public SettingsViewModel()
        {
            AppVersion = $"WpfMvvmTemplate.WPFUI - {GetAssemblyVersion()}";
        }

        public void OnNavigatedTo()
        {
            if(!isInitialized)
                InitializeViewModel();
        }

        public void OnNavigatedFrom() { }

        private void InitializeViewModel()
        {
            //读取文件内容
            string fileContent = File.ReadAllText(GlobalStaticData.AppSettingsFilePath);

            //解析文件内容
            JObject? jObject = JObject.Parse(fileContent);

            switch(jObject?["AppSettings"]?["Theme"]!.ToString())
            {
                case "0": CurrentTheme = ApplicationTheme.Light;  break;
                case "1": CurrentTheme = ApplicationTheme.Dark;  break;
            }

            AppVersion = $"WpfMvvmTemplate.WPFUI - {GetAssemblyVersion()}";

            isInitialized = true;
        }

        private string GetAssemblyVersion()
        {
            return Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? string.Empty;
        }

        /// <summary>
        /// 切换主题颜色
        /// </summary>
        /// <param name="parameter"></param>
        [RelayCommand]
        private void ChangeTheme(string parameter)
        {
            //读取文件内容
            string fileContent = File.ReadAllText(GlobalStaticData.AppSettingsFilePath);

            //解析文件内容
            JObject? jObject = JObject.Parse(fileContent);
            JToken? jToken = jObject?["AppSettings"]?["Theme"];

            switch(parameter)
            {
                case "0":
                case "theme_light":
                    if(CurrentTheme == ApplicationTheme.Light) break;

                    jToken?.Replace(JToken.FromObject("0"));
                    ApplicationThemeManager.Apply(ApplicationTheme.Light);
                    CurrentTheme = ApplicationTheme.Light;
                    break;

                case "1":
                case "theme_dark":
                    if(CurrentTheme == ApplicationTheme.Dark) break;

                    jToken?.Replace(JToken.FromObject("1"));
                    ApplicationThemeManager.Apply(ApplicationTheme.Dark);
                    CurrentTheme = ApplicationTheme.Dark;
                    break;
            }

            //保存中英文到配置文件中
            File.WriteAllText(GlobalStaticData.AppSettingsFilePath, jObject?.ToString());
        }

    }
}
